package com.hp.attendance.service;



import com.hp.attendance.dao.OneselfDao;
import com.hp.attendance.entity.*;
import com.hp.attendance.entity.dto.EmpOverDto;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OneselfService {
    private OneselfDao oneselfDao = new OneselfDao();

    //根据id查询员工信息
    //预修改使用
    public Employee getOneselfOneById(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Employee employee = oneselfDao.getOneselfOneById(id);
        return employee;
    }

    //修改员工信息
    public void UpdateOneself(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        oneselfDao.UpdateOneself(employee);
    }

    //根据员工编号查询考勤记录
    public List<Attendance> getOneselfAttendance(int employee_number) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return oneselfDao.getOneselfAttendance(employee_number);
    }

    //请假申请
    public void insertOneselfLeave(Leave leave) throws SQLException, IOException, ClassNotFoundException {
        oneselfDao.insertOneselfLeave(leave);
    }
    //查询请假时间是否存在
    public List<Leave> getLeaveTime(int employeeNumber , Date startTime, Date endTime) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<Leave> list = oneselfDao.getLeaveTime(employeeNumber, startTime, endTime);
        return list;
    }

    //个人请假数据分页查询
    public PageInfo<Leave> getOneselfPage(int employeeNumber , int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//        PageInfo<Leave> page = new PageInfo<>();
        PageInfo<Leave> page = new PageInfo<>();
        List<Leave> list = oneselfDao.getOneselfLeaveList(employeeNumber, pageNo, pageSize);

        //存入查询到的分页数据
        page.setRecords(list);

        //查询记录条数
        long total = oneselfDao.getTotal(employeeNumber);
        //存入
        page.setTotal(total);

        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    //个人请假数据分页查询
    public PageInfo<EmpOverDto> getOneselfOvertimePage(int employeeNumber , int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<EmpOverDto> page = new PageInfo<>();

        List<EmpOverDto> list = new ArrayList<>();
        List<Overtime> overtime = oneselfDao.getOneselfOvertimeList(employeeNumber, pageNo, pageSize);
        for (Overtime o : overtime) {
            EmpOverDto empOverDto = new EmpOverDto();
            Employee employee = oneselfDao.getEmployeeByEN(o.getEmployee_number());
            Department department = oneselfDao.getDepartmentByDN(o.getDepartment_number());
            empOverDto.setEmployee(employee);
            empOverDto.setDepartment(department);
            empOverDto.setOvertime(o);
            list.add(empOverDto);
        }
        //存入查询到的分页数据
        page.setRecords(list);

        //查询记录条数
        long total = oneselfDao.getOvertimeTotal(employeeNumber);
        //存入
        page.setTotal(total);

        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    //根据部门编号查询部门信息
    public Department getDepartmentByDN(int departmentNumber) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return oneselfDao.getDepartmentByDN(departmentNumber);
    }

    //根据职位编号查询职位信息
    public Position getPositionByPN(int positionNumber) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return oneselfDao.getPositionByPN(positionNumber);
    }

    /**
     * 根据id查询个人
     */
    public Employee getEmployee(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Employee employee = oneselfDao.getEmployee(id);
        return employee;
    }

    /**
     * 根据id查询部门
     */
    public Department getDepartment(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Department department = oneselfDao.getDepartment(id);
        return department;
    }
}
